﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
using System.Data.Objects;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblBrandDAL:ImtblBrand
    {
        HKEA_CMSEntities model;
        mtblBrand brand;
        public mtblBrandDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblBrandDAL GetInstance()
        {      
            return new mtblBrandDAL();
        }

        public mtblBrand InsertENT(mtblBrand ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblBrands.AddObject(ent);
            if (model.SaveChanges() > 0)
                 ent= model.mtblBrands.OrderBy(a=>a.BrandUID).ToList().Last();
            return ent;
        }

        public List<mtblBrand> GetListByENT(mtblBrand ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblBrands.Where(a => a.BrandUID > 0);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.BrandUID > 0)
                result = result.Where(a => a.BrandUID == ent.BrandUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<BrandListDetail> GetListDetailByENT(mtblBrand ent)
        {
            var result = from brand in model.mtblBrands
                         // join brand_cat in model.tblBrand_ProdCat
                         //on brand.BrandUID equals brand_cat.BrandUID into tempTable
                         //from temp in tempTable.DefaultIfEmpty()
                         select new BrandListDetail {
                             BrandLogo=brand.BrandLogo,
                             CustomerUID=brand.CustomerUID,
                            BrandUID=brand.BrandUID,
                            BrandName_Chi=brand.BrandName_Chi,
                            BrandName_Eng=brand.BrandName_Eng,
                            Deleted=brand.Deleted
                         };
           // var str = (result as ObjectQuery<BrandListDetail>).ToTraceString();
            if (ent.CustomerUID > 0)
                result = result.Where(a=>a.CustomerUID ==ent.CustomerUID);
            if (ent.BrandUID > 0)
                result = result.Where(a=>a.BrandUID ==ent.BrandUID);
             result= result.Where(a=>a.Deleted==ent.Deleted);
             return result.ToList();

        }
        public List<M_BrandListDetail> GetListByCustUID(List<int> ids, int custUID)
        {
            var ret = from brand in model.mtblBrands
                      where brand.CustomerUID == custUID && brand.Deleted==0
                      select new M_BrandListDetail
                      {
                          AppBrandLogo = 0,
                          AppBrandUID = 0,
                          BrandLogo = brand.BrandLogo > 0 ? (int)brand.BrandLogo : 0,
                          BrandName_Chi = brand.BrandName_Chi,
                          BrandName_Eng = brand.BrandName_Eng,
                          BrandUID = brand.BrandUID,
                          Deleted = 0,
                          IsFromApp = false
                      };
            if (ids != null && ids.Count > 0)
                ret = ret.Where(a=>!ids.Contains(a.BrandUID));
            return ret.ToList();
        }

        public List<mtblBrand> GetListByENT(mtblBrand ent)
        {
            var result = model.mtblBrands.Where(a => a.BrandUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.BrandUID > 0)
                result = result.Where(a=>a.BrandUID ==ent.BrandUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(mtblBrand ent)
        {
            var result = model.mtblBrands.Where(a => a.BrandUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.BrandUID > 0)
                result = result.Where(a => a.BrandUID == ent.BrandUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblBrand ent)
        {
            brand = model.mtblBrands.Where(a => a.BrandUID == ent.BrandUID).FirstOrDefault();
            if (brand != null)
            {
                brand.Deleted = ent.Deleted;
                brand.BrandLogo = ent.BrandLogo;
                brand.BrandName_Chi = ent.BrandName_Chi;
                brand.BrandName_Eng = ent.BrandName_Eng;
                brand.Intro_Chi = ent.Intro_Chi;
                brand.Intro_Eng = ent.Intro_Eng;
                brand.LastModifyBy = ent.LastModifyBy;
                brand.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblBrand ent)
        {
            brand = model.mtblBrands.Where(a => a.BrandUID == ent.BrandUID).FirstOrDefault();
            if (brand != null)
            {
                model.mtblBrands.DeleteObject(brand);
            }
            return model.SaveChanges();
        }
    }
}
